package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.hjq.base.BaseDialog;
import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.QueryCodeResultBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.activity.BrowserActivity;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class DeviceSettingAngleStroke extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;
    private String queryCode;
    private int totalTime = 20 * 1000;
    private long startTime;

    public DeviceSettingAngleStroke(AppActivity context, DeviceBean deviceBean, String title) {
        this(context, null, deviceBean, title);
    }

    public DeviceSettingAngleStroke(AppActivity context, AttributeSet attrs, DeviceBean deviceBean, String title) {
        this(context, attrs, 0, deviceBean, title);
    }

    public DeviceSettingAngleStroke(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean, String title) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean, title);
    }

    public DeviceSettingAngleStroke(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean, String title) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(title);
        setOnClickListener(this::onClick);
    }
    @Override
    public void onClick(View v) {
        showStartStopHintDialog();
    }

    private BaseDialog mConfirmCancelHintDialog;
    private ConfirmCancelHintDialog.Builder mConfirmCancelHintDialogBuilder;
    private void showStartStopHintDialog() {
        if (mConfirmCancelHintDialog == null) {
            mConfirmCancelHintDialogBuilder = new ConfirmCancelHintDialog.Builder(mContext);
            mConfirmCancelHintDialogBuilder.setRightText(R.string.common_confirm);
            mConfirmCancelHintDialog = mConfirmCancelHintDialogBuilder.create();
        }
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                beginOperation();
            }
        });
        mConfirmCancelHintDialogBuilder.setContent("1、确认窗帘已经设置过开合行程；2、确认窗帘是关闭状态；");
        mConfirmCancelHintDialog.show();
    }

    private void showSecondDialog() {
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                secondOperation();
            }
        });
        mConfirmCancelHintDialogBuilder.setContent("确认窗帘开合一次，电机进入设置状态");
        mConfirmCancelHintDialog.show();
    }


    private void beginOperation(){
        Map<String, Object> option = new HashMap<>();
        option.put("angle_travelsetting", 5);
        Map<String, Object> status = new HashMap<>();
        status.put("settings", option);
        LLHttpManager.sendDeviceSetting((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<QueryCodeResultBean<String, Object>>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<QueryCodeResultBean<String, Object>> result) {
                showSecondDialog();
            }

            @Override
            public void onFail(Exception e) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ToastUtils.show(R.string.exec_failure);
                }
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    private void secondOperation(){
        Map<String, Object> option = new HashMap<>();
        option.put("angle_travelsetting", 1);
        Map<String, Object> status = new HashMap<>();
        status.put("settings", option);
        LLHttpManager.sendDeviceSetting((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<QueryCodeResultBean<String, Object>>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<QueryCodeResultBean<String, Object>> result) {
                showConfirmHintDialog();
            }

            @Override
            public void onFail(Exception e) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ToastUtils.show(R.string.exec_failure);
                }
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    private BaseDialog mConfirmHintDialog;
    private ConfirmHintDialog.Builder mConfirmHintDialogBuilder;
    private void showConfirmHintDialog(){
        if(mConfirmHintDialog == null){
            mConfirmHintDialogBuilder = new ConfirmHintDialog.Builder(getContext());
            mConfirmHintDialog = mConfirmHintDialogBuilder.create();
        }
        mConfirmHintDialogBuilder.setContent("角度行程设置完成，请等待窗帘反馈...");
        mConfirmHintDialog.show();
    }
}